package com.usian.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.pojos.WmUser;
import com.usian.model.user.dtos.AuthDto;
import com.usian.model.user.pojos.ApUser;
import com.usian.model.user.pojos.ApUserRealname;
import com.usian.user.feign.ApAuthorFeign;
import com.usian.user.feign.WmUserFeign;
import com.usian.user.mapper.ApUserMapper;
import com.usian.user.mapper.ApUserRealnameMapper;
import com.usian.user.service.ApUserRealnameService;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {

    public static final Short PASS_AUTH = 9;
    public static final Short FAIL_AUTH = 2;

    @Autowired
    private ApUserMapper apUserMapper;
    @Autowired
    private WmUserFeign wmUserFeign;
    @Autowired
    private ApAuthorFeign apAuthorFeign;

    @Override
    public ResponseResult findByStatus(AuthDto dto) {

        //参数校验
        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.hashCode();

        QueryWrapper<ApUserRealname> queryWrapper = new QueryWrapper();
        if (dto.getStatus() != null){
            queryWrapper.lambda().eq(ApUserRealname::getStatus,dto.getStatus());
        }

        Page page = new Page(dto.getPage(), dto.getSize());

        IPage ipage = page(page,queryWrapper);

        ResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) ipage.getTotal());
        responseResult.setData(ipage.getRecords());

        return responseResult;
    }

    @Override
    @GlobalTransactional
    public ResponseResult updateStatusById(AuthDto dto, Short status) {

        //校验参数
        if (dto == null || status == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if (status!=9 && status!=2){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        ApUserRealname apUserRealname = this.getById(dto.getId());

        //更新apUserRealname表中数据
        ApUserRealname userRealname = new ApUserRealname();

        userRealname.setId(dto.getId());
        userRealname.setStatus(status);

        if (dto.getMsg()!= null){
            userRealname.setReason(dto.getMsg());
        }

        //将当前时间添加未修改时间
        userRealname.setUpdatedTime(new Date());

        //根究id进行修改
        this.updateById(userRealname);

        //判断审核状态
        if (status.equals(PASS_AUTH)){
            ResponseResult wmUserAndApauthor = this.createWmUserAndApauthor(dto, apUserRealname.getUserId());
            if (wmUserAndApauthor.getCode()!=0){
                return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
            }

            //更新apuser中数据
            ApUser apUser = new ApUser();
            apUser.setId(apUserRealname.getUserId());
            apUser.setFlag(Short.valueOf("1"));

            apUserMapper.updateById(apUser);

        }


        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    //添加数据
    private ResponseResult createWmUserAndApauthor(AuthDto dto,Integer userId){

        ApUser apUser = apUserMapper.selectById(userId);

        //判断数据是否为空
        if (apUser == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        //根据name查询
        WmUser byName = wmUserFeign.findByName(apUser.getName());

        if (byName == null){

            WmUser wmUser = new WmUser();

            wmUser.setApUserId(apUser.getId());
            wmUser.setCreatedTime(new Date());
            wmUser.setSalt(apUser.getSalt());
            wmUser.setName(apUser.getName());
            wmUser.setStatus(9);
            wmUser.setPhone(apUser.getPhone());
            wmUser.setPassword(apUser.getPassword());

            WmUser wmUser1 = wmUserFeign.saveWmUser(wmUser);

            Integer apUserId = apUser.getId();

            //通过userId查询
            ApAuthor author = apAuthorFeign.findByUserId(apUserId);

            //判断author对象是否为空，若为空则添加数据
            if (author == null){
                ApAuthor apAuthor = new ApAuthor();

                apAuthor.setType(2);
                apAuthor.setCreatedTime(new Date());
                apAuthor.setName(wmUser1.getName());
                apAuthor.setWmUserId(wmUser1.getId());
                apAuthor.setUserId(userId);
                apAuthorFeign.saveApAuthor(apAuthor);

            }

        }

        return ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS);
    }


}
